<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton, ElTag,ElRow,ElCol,ElTree } from 'element-plus'
import { Table } from '@/components/Table'
import { getTableListApi, delTableListApi } from '@/api/table'
import { useTable } from './vCircle/web/vCircleTable'
import {h, reactive, ref} from 'vue'
import { useRouter } from 'vue-router'
import { useEmitt } from '@/hooks/web/useEmitt'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { TableColumn } from '@/types/table'
import { getQianTaiYongHuDengLuRiZhi , delectQianTaiYongHuDengLuRiZhi} from '@/views/QuanXian/vCircle/table/index'
import { inject } from "vue";


defineOptions({
  name: 'ExamplePage'
})

const { push } = useRouter()

const { register, tableObject, methods } = useTable({
  getListApi: getQianTaiYongHuDengLuRiZhi,
  delListApi: delTableListApi,
  response: {
    list: 'list',
    total: 'total'
  }
})

const { getList, setSearchParams } = methods

getList()

useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      tableObject.currentPage = 1
    }
    getList()
  }
})

const { t } = useI18n()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'id',
    label: "序号",
    type: 'id'
  },
  {
    field: 'loginIp',
    label: "登录地址"
  },
  {
    field: 'loginLocation',
    label: "登录IP"
  },
  {
    field: 'loginState',
    label: "登录状态",
    formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
      return h(
          ElTag,
          {
            type: cellValue === 2 ? 'success' : cellValue === 3 ? 'warning' : 'danger'
          },
          () =>
              cellValue === 2
                  ? "登录成功"
                  : "登陆失败"
      )
    }
  },
  {
    field: 'loginDate',
    label: "登录时间",
  },
  {
    field: 'trueName',
    label: "名称"
  },
  {
    field: 'account',
    label: "账号"
  },
  {
    field: 'action',
    label: "操作"
  },
])


const { allSchemas } = useCrudSchemas(crudSchemas)

//批量删除
const delLoading = ref(false)

//局部刷新
const ceshi = inject("reload");
//行内删除
const delData = async (row: TableData | null, multiple: boolean) => {
  delectQianTaiYongHuDengLuRiZhi({"id" : row.id}).then((res) =>{
    if(res){
      getList()
    }
  })
}

//表单查询
import { Form } from '@/components/Form'
import {FormSchema} from "@/types/form";
import {TableData} from "@/views/QuanXian/vCircle/table/types";
const schema = reactive<FormSchema[]>([
  {
    field: 'account',
    label: '账号',
    component: 'Input'
  },
  {
    field: 'trueName',
    label: '名称',
    component: 'Input'
  }
])

</script>

<template>

  <ElRow class="row-con">
    <ElCol :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
      <ContentWrap>
        <Search
            :expand = "true"
            :schema="schema"
            :expandField = str
            @search= "setSearchParams"
            @reset= "setSearchParams"
        />

        <Table
            v-model:pageSize="tableObject.pageSize"
            v-model:currentPage="tableObject.currentPage"
            :columns="allSchemas.tableColumns"
            :data="tableObject.tableList.slice((tableObject.currentPage-1) * tableObject.pageSize, tableObject.currentPage * tableObject.pageSize)"
            :loading="tableObject.loading"
            :pagination="{
        total: tableObject.total
      }"
            @register="register"
        >
          <template #action="{ row }">
            <ElButton type="danger" @click="delData(row, false)">
              {{ t('删除') }}
            </ElButton>
          </template>
        </Table>
      </ContentWrap>
    </ElCol>
  </ElRow>


</template>
